package com.imoblife.brainwavestus.product;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Stack;

import com.imoblife.brainwavestus.model.Constant;
import com.imoblife.brainwavestus.util.DebugUtil;

public class Store {
	
	public static final String TAG = "Store";
	public HashMap<String, ArrayList<Product>> mCategory;
	public HashMap<String, Product> mProducts;
	public ArrayList<String> mHotSaleGroup;
	public ArrayList<String> mNewGroup;
	public ArrayList<String> mDiscountGroup;
	
	private Stack<String> mDownloadingTask;
	
	public Store() {
		
		mCategory = new HashMap<String, ArrayList<Product>>();
		mProducts = new HashMap<String, Product>();
		mHotSaleGroup = new ArrayList<String>();
		mNewGroup = new ArrayList<String>();
		mDiscountGroup = new ArrayList<String>();
		mDownloadingTask = new Stack<String>();
	}
	
	public void clearProduct() {
		
		if(mCategory != null) {
			
			mCategory.clear();
		}
		
		if(mProducts != null) {
			
			mProducts.clear();
		}
		
		if(mHotSaleGroup != null) {
			
			mHotSaleGroup.clear();
		}
		
		if(mNewGroup != null) {
			
			mNewGroup.clear();
		}
		
		if(mDiscountGroup != null) {
			
			mDiscountGroup.clear();
		}
	}
	
	public Stack<String> getDownloadingTask() {
		
		return mDownloadingTask;
	}
	
	public void addDownloadTaskToStack(String productId) {
		
		if(mDownloadingTask != null) {
			
			mDownloadingTask.push(productId);
		}		
	}
	
	//use peek method, so can not remove the element in the stack
	public String getRecentDownloadTask() {
		
		if(mDownloadingTask != null 
				&& mDownloadingTask.size() > 0) {
		
			return mDownloadingTask.peek();
		}
		
		return null;
	}
	
	// use peek method, so can not remove the element in the stack
	public String removeTopTask() {

		if (mDownloadingTask != null && mDownloadingTask.size() > 0) {

			return mDownloadingTask.pop();
		}

		return null;
	}
		
	public boolean updateProduct() {
		
		return false;
	}
	
	public void addProduct(String productName, Product product) {
		
		mProducts.put(productName, product);
	}
	
	public boolean addProductToCategory(String category, Product product) {
		
		ArrayList<Product> productList;
		
		if(mCategory != null && mCategory.containsKey(category)) {
			
			productList = mCategory.get(category);
		}else {
			
			productList = new ArrayList<Product>();
			mCategory.put(category, productList);
		}
		
		if(productList != null){
			
			return productList.add(product);
		}
		
		return false;
	}
	
	public boolean addHotSaleProduct(String productName) {
		
		if(null != mHotSaleGroup) {
			
			return mHotSaleGroup.add(productName);
		}
		return false;
	}
	
	public boolean addNewProduct (String productName) {
		
		if(null != mNewGroup) {
			
			return mNewGroup.add(productName);
		}
		
		return false;
	}
	
	public boolean addGroupProduct(int group, String productName) {
		
		switch(group){
		
		case Constant.GROUP_NEW :
			if(null != mNewGroup) {
				return mNewGroup.add(productName);
			}
			break;
			
		case Constant.GROUP_DISCOUNT:
			
			if(null != mDiscountGroup) {
				return mDiscountGroup.add(productName);
			}
			break;
			
		case Constant.GROUP_HOT_SALE:
			
			if(null != mHotSaleGroup) {
				
				return mHotSaleGroup.add(productName);
			}
			break;
		
		default:
			return false;
		}
		
		return false;
	}
	
	public ArrayList<Product> getProductByCategory(String category) {
		
		if(null == mCategory) {
			
			return null;
		}
		
		if(mCategory.containsKey(category)) {
			
			return mCategory.get(category);
		}else {
			
			ArrayList<Product> allproducts 
			 			= new ArrayList<Product>();	
			
			Collection<ArrayList<Product>> collections
						= mCategory.values();		
			
			for(ArrayList<Product> datas:collections) {
				
				allproducts.addAll(datas);
			}
						
			return allproducts;
		}		
	}
	
	public Product getProductByName(String productName) {
		
		if(null != mProducts) {
			
			return mProducts.get(productName);
		}
		
		return null;
	}
	
	public ArrayList<Product> getSaleGroupProducts(int groupType) {
		
		ArrayList<Product> groupProducts = 
						new ArrayList<Product>();	
		ArrayList<String> saleGroupData = null;
		
		switch(groupType){
			
		case Constant.GROUP_DISCOUNT:
			saleGroupData = mDiscountGroup;
			break;
		
		case Constant.GROUP_NEW:
			saleGroupData = mNewGroup;
			break;
		
		case Constant.GROUP_HOT_SALE:
			saleGroupData = mHotSaleGroup;
			break;
		}
		
		if(saleGroupData == null) {
			
			DebugUtil.dbDebug(TAG, "can not get the group data,please check getSaleGroupProducts method");
			
			return null;
		}
		
		int size = saleGroupData.size();
		
		DebugUtil.dbDebug("Store","hotSale product size:" + size);
		
		for (int i = 0;i < size ;i++) {
			
			String productName = saleGroupData.get(i);
			Product product = getProductByName(productName);
			
			DebugUtil.dbDebug("Store"," product name:" + productName);
			
			if (product != null) {
				
				groupProducts.add(product);
				
				DebugUtil.dbDebug("Store","good boy,add hotSale product success!!!");
			}		
		}
		
		return groupProducts;
	}
	
	public ArrayList<Product> getAllProducts() {
		
		if(null != mProducts) {
			
			return new ArrayList<Product>(mProducts.values());
		}
		
		return null;
	}
	
	public ArrayList<String> getAllCategories() {
		
		if(mCategory != null) {
			
			return new ArrayList<String>(mCategory.keySet());
		}
		
		return null;
	}
	
	public ArrayList<String> getHotSaleProductNames() {
		
		return mHotSaleGroup;
	}
	
	public ArrayList<String> getDiscountProductNames() {
		
		return mDiscountGroup;
	}
	
	public ArrayList<String> getNewProductNames() {
		
		return mNewGroup;
	}
}
